package com.example.myapplication.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.tack_05.Exit;
import com.example.myapplication.tack_05.Feedback;
import com.example.myapplication.tack_05.Person;
import com.example.myapplication.tack_05.SetPassword;
import com.example.myapplication.tack_05.data.SetPersonData;
import com.example.myapplication.utils.OkHttpTool;

import java.util.Set;


public class PersonFragment extends Fragment {
    private View view;
    private LinearLayout person;
    private LinearLayout order;
    private LinearLayout password;
    private LinearLayout feedback;
    private TextView exit;
    private TextView username;
    private String name;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private String phe;
    private String sex;
    private String password1;
    private Handler handler;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_person, container, false);
        init();
        //个人信息
        getPerson();
        //订单
        getOrder();
        //修改密码
        getPassword();
        //意见反馈
        getFeedback();
        //退出登录
        getExit();
        return view;
    }

    private void getExit() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if(!"".equals(OkHttpTool.TOKEN)){
                            editor.putString("token", "");
                            editor.apply();
                            OkHttpTool.TOKEN = "";
                            exit.setText("登录");
                            username.setText("未登录");

                        }else{
                            exit.setText("退出登录");
                            Intent intent = new Intent(PersonFragment.this.getContext(), Exit.class);
                            PersonFragment.this.getContext().startActivity(intent);
                        }

                    }
                });

            }
        });
    }

    private void getFeedback() {
        feedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if("".equals(OkHttpTool.TOKEN)){
                    Toast.makeText(PersonFragment.this.getContext(),"请先登录账号",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(PersonFragment.this.getContext(), Exit.class);
                    PersonFragment.this.getContext().startActivity(intent);
                }else{
                    Intent intent = new Intent(PersonFragment.this.getContext(), Feedback.class);
                    PersonFragment.this.getContext().startActivity(intent);
                }

            }
        });
    }

    private void getPassword() {

        password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if("".equals(OkHttpTool.TOKEN)){
                    Toast.makeText(PersonFragment.this.getContext(),"请先登录账号",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(PersonFragment.this.getContext(), Exit.class);
                    PersonFragment.this.getContext().startActivity(intent);
                }else{
                    Intent intent = new Intent(PersonFragment.this.getContext(), SetPassword.class);
                    PersonFragment.this.getContext().startActivity(intent);
                }

            }
        });
    }

    private void getOrder() {
    }

    private void getPerson() {
        person.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if("".equals(OkHttpTool.TOKEN)){
                    Toast.makeText(PersonFragment.this.getContext(),"请先登录账号",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(PersonFragment.this.getContext(), Exit.class);
                    PersonFragment.this.getContext().startActivity(intent);
                }else{
                    Intent intent = new Intent(PersonFragment.this.getContext(), Person.class);
                    PersonFragment.this.getContext().startActivity(intent);
                }


            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        if("".equals(OkHttpTool.TOKEN)){
            username.setText("未登录");
        }else {
            sharedPreferences = getActivity().getSharedPreferences("login", Context.MODE_PRIVATE);
            editor = sharedPreferences.edit();
            name = sharedPreferences.getString("username", "");
            phe = sharedPreferences.getString("phonenumber", "");
            sex = sharedPreferences.getString("sex", "");
            password1 = sharedPreferences.getString("password", "");
            if(OkHttpTool.TOKEN != null){
                username.setText(name);
            }

        }

    }

    private void init() {
        person = view.findViewById(R.id.person);
        order = view.findViewById(R.id.order);
        password = view.findViewById(R.id.password);
        feedback = view.findViewById(R.id.feedback);
        exit = view.findViewById(R.id.exit);
        username = view.findViewById(R.id.username);
        handler = new Handler();
    }

}